package com.example.infrastructure.persistent.repository;

import com.example.domain.activity.model.entity.ActivityCountEntity;
import com.example.domain.activity.model.entity.ActivityEntity;
import com.example.domain.activity.model.entity.ActivitySkuEntity;
import com.example.domain.activity.model.valobj.ActivityStateVO;
import com.example.domain.activity.repository.IActivityRepository;
import com.example.infrastructure.persistent.dao.IRaffleActivityCountDao;
import com.example.infrastructure.persistent.dao.IRaffleActivityDao;
import com.example.infrastructure.persistent.dao.IRaffleActivitySkuDao;
import com.example.infrastructure.persistent.po.RaffleActivityCountPO;
import com.example.infrastructure.persistent.po.RaffleActivityPO;
import com.example.infrastructure.persistent.po.RaffleActivitySkuPO;
import com.example.infrastructure.persistent.redis.IRedisService;
import com.example.types.common.Constants;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @Author: 三石
 * @desc: 活动仓储实现
 * @Date: 2024-07-29-18:11
 */
@Repository
public class ActivityRepositoryImpl implements IActivityRepository {

    @Resource
    private IRedisService redisService;
    @Resource
    private IRaffleActivityDao raffleActivityDao;
    @Resource
    private IRaffleActivitySkuDao raffleActivitySkuDao;
    @Resource
    private IRaffleActivityCountDao raffleActivityCountDao;

    /**
     * 通过sku查询sku信息
     * @param sku sku ID
     * @return sku 实体
     */
    @Override
    public ActivitySkuEntity queryActivitySku(Long sku) {
        RaffleActivitySkuPO raffleActivitySku = raffleActivitySkuDao.queryActivitySku(sku);
        return ActivitySkuEntity.builder()
                .sku(raffleActivitySku.getSku())
                .activityId(raffleActivitySku.getActivityId())
                .activityCountId(raffleActivitySku.getActivityCountId())
                .stockCount(raffleActivitySku.getStockCount())
                .stockCountSurplus(raffleActivitySku.getStockCountSurplus())
                .build();
    }

    /**
     * 通过活动ID查询活动信息
     * @param activityId 活动ID
     * @return 活动实体
     */
    @Override
    public ActivityEntity queryRaffleActivityByActivityId(Long activityId) {
// 优先从缓存获取
        String cacheKey = Constants.RedisKey.ACTIVITY_KEY + activityId;
        ActivityEntity activityEntity = redisService.getValue(cacheKey, ActivityEntity.class);
        if (null != activityEntity) return activityEntity;
        // 从库中获取数据
        RaffleActivityPO raffleActivity = raffleActivityDao.queryRaffleActivityByActivityId(activityId);
        activityEntity = ActivityEntity.builder()
                .activityId(raffleActivity.getActivityId())
                .activityName(raffleActivity.getActivityName())
                .activityDesc(raffleActivity.getActivityDesc())
                .beginDateTime(raffleActivity.getBeginDateTime())
                .endDateTime(raffleActivity.getEndDateTime())
                .strategyId(raffleActivity.getStrategyId())
                .state(ActivityStateVO.valueOf(raffleActivity.getState()))
                .build();
        redisService.setValue(cacheKey, activityEntity);
        return activityEntity;
    }

    /**
     *  查询次数信息（用户在活动上可参与的次数）
     * @param activityCountId 活动次数配置ID
     * @return 次数信息实体
     */
    @Override
    public ActivityCountEntity queryRaffleActivityCountByActivityCountId(Long activityCountId) {
        // 优先从缓存获取
        String cacheKey = Constants.RedisKey.ACTIVITY_COUNT_KEY + activityCountId;
        ActivityCountEntity activityCountEntity = redisService.getValue(cacheKey, ActivityCountEntity.class);
        if (null != activityCountEntity) return activityCountEntity;
        // 从库中获取数据
        RaffleActivityCountPO raffleActivityCount = raffleActivityCountDao.queryRaffleActivityCountByActivityCountId(activityCountId);
        activityCountEntity = ActivityCountEntity.builder()
                .activityCountId(raffleActivityCount.getActivityCountId())
                .totalCount(raffleActivityCount.getTotalCount())
                .dayCount(raffleActivityCount.getDayCount())
                .monthCount(raffleActivityCount.getMonthCount())
                .build();
        redisService.setValue(cacheKey, activityCountEntity);
        return activityCountEntity;
    }
}
